"""Interface for Aladin Pro"""

import utils
import serial
import sys

def main():
    """Main function"""
    
    #Look for ports
    ports = utils.scan()
    availnumbers = []
    print "Found ports:"
    for number, name in ports:
        print "(%d) %s" % (number, name)
        availnumbers.append(number)
    
    #Select port number
    n = None
    while n == None:
        try:
            print "Please select the port number:"
            ns = sys.stdin.readline()
            n = int(ns)
            
            if n not in availnumbers:
                print "%d not a valid port!" % n
                n = None
                
        except ValueError:
            print "%s not a number!" % ns.rstrip('\r\n')
            n = None
            
    print "Port %d selected" % n
    
    s = serial.Serial(n, 19200, 8, 'N', 1)
    print "Port %s opened" % s.name
    
    print "Waiting for connection..."
    s.setTimeout(5)
    while len(s.read(1)) == 0:
        pass
    
    print "Aladin pro connected!!!"
    
    
    s.close()
    print "Port %s closed" % s.name
if __name__ == "__main__":
    main()